//! # 配置验证器实现
//! 
//! 本模块提供了RustCloud框架的配置验证器实现。

use crate::advanced_builder::traits::*;
use crate::config::RustCloudConfig;
use rustcloud_core::{ServiceResult, ServiceError};

/// 基础配置验证器
pub struct BasicConfigValidator;

#[async_trait::async_trait]
impl ConfigValidator for BasicConfigValidator {
    async fn validate(&self, config: &RustCloudConfig) -> ServiceResult<()> {
        if config.service.name.is_empty() {
            return Err(ServiceError::ConfigError("服务名称不能为空".to_string()));
        }
        
        if config.network.port == 0 {
            return Err(ServiceError::ConfigError("服务端口不能为0".to_string()));
        }
        
        Ok(())
    }

    fn name(&self) -> &str {
        "basic"
    }
}

/// 安全配置验证器
pub struct SecurityConfigValidator;

#[async_trait::async_trait]
impl ConfigValidator for SecurityConfigValidator {
    async fn validate(&self, config: &RustCloudConfig) -> ServiceResult<()> {
        // 验证安全配置
        if let Some(ref security) = config.security {
            if let Some(ref auth) = security.auth {
                match auth.auth_type.as_str() {
                    "basic" => {
                        if auth.username.is_none() || auth.password.is_none() {
                            return Err(ServiceError::ConfigError("Basic认证需要用户名和密码".to_string()));
                        }
                    },
                    "bearer" => {
                        if auth.token.is_none() {
                            return Err(ServiceError::ConfigError("Bearer认证需要Token".to_string()));
                        }
                    },
                    "jwt" => {
                        // JWT验证可以更复杂，这里简化处理
                    },
                    _ => {
                        return Err(ServiceError::ConfigError(format!("不支持的认证类型: {}", auth.auth_type)));
                    }
                }
            }
        }
        
        Ok(())
    }

    fn name(&self) -> &str {
        "security"
    }
}

/// 性能配置验证器
pub struct PerformanceConfigValidator;

#[async_trait::async_trait]
impl ConfigValidator for PerformanceConfigValidator {
    async fn validate(&self, config: &RustCloudConfig) -> ServiceResult<()> {
        // 验证连接池配置
        if config.performance.connection_pool.max_connections < config.performance.connection_pool.min_connections {
            return Err(ServiceError::ConfigError("最大连接数不能小于最小连接数".to_string()));
        }
        
        // 验证线程池配置
        if config.performance.thread_pool.max_threads < config.performance.thread_pool.core_threads {
            return Err(ServiceError::ConfigError("最大线程数不能小于核心线程数".to_string()));
        }
        
        Ok(())
    }

    fn name(&self) -> &str {
        "performance"
    }
}

/// 网络配置验证器
pub struct NetworkConfigValidator;

#[async_trait::async_trait]
impl ConfigValidator for NetworkConfigValidator {
    async fn validate(&self, config: &RustCloudConfig) -> ServiceResult<()> {
        // 验证端口范围
        if config.network.port < 1 || config.network.port > 65535 {
            return Err(ServiceError::ConfigError("服务端口必须在1-65535范围内".to_string()));
        }
        
        if config.network.health_port < 1 || config.network.health_port > 65535 {
            return Err(ServiceError::ConfigError("健康检查端口必须在1-65535范围内".to_string()));
        }
        
        // 验证绑定地址格式
        if !config.network.bind_address.is_empty() && config.network.bind_address != "0.0.0.0" {
            // 这里可以添加更严格的IP地址验证
            if !config.network.bind_address.parse::<std::net::IpAddr>().is_ok() {
                return Err(ServiceError::ConfigError("绑定地址格式不正确".to_string()));
            }
        }
        
        Ok(())
    }

    fn name(&self) -> &str {
        "network"
    }
}